﻿@namespace BootstrapBlazor.Components
@typeparam TItem where TItem : class, new()
@using FreeSql.Extensions.EntityUtil
@using FreeSql.Internal.Model
@using System.Collections

@{
    var selectedCount = items.Where(a => a.Selected).Count();
    var is1r4c = TableTd1 != null && TableRow == null && TableHeader == null && IsEdit == false && IsRemove == false;
}
<div class="card @(is1r4c ? "" : "card-info ")card-outline">
    @if (q.Filters.Any() == true)
    {
        <div class="card-header d-block">
            <AdminSearchFilter AdminQuery="q" />
        </div>
    }
    @if (IsRemove || IsEdit && IsSingleSelect || IsAdd || CardHeader != null || IsSearchText)
    {
        <div class="card-header d-block">
            @if (IsRemove && (IsMultiSelect || IsSingleSelect))
            {
                if (selectedCount > 0)
                {
                    <button @onclick="e => Delete()" type="button" class="mr-2 btn btn-light btn-sm"><i class="far fa-trash-alt"></i></button>
                }
                else
                {
                    <button type="button" class="mr-2 btn btn-light btn-sm disabled"><i class="far fa-trash-alt"></i></button>
                }
            }
            @if (IsEdit && IsSingleSelect)
            {
                if (selectedCount > 0)
                {
                    <button @onclick="e => BeginEdit(items.FirstOrDefault(a => a.Selected)?.Value)" type="button" class="mr-2 btn btn-light btn-sm"><i class="far fa-edit"></i></button>
                }
                else
                {
                    <button type="button" class="mr-2 btn btn-light btn-sm disabled"><i class="far fa-edit"></i></button>
                }
            }
            @if (IsAdd)
            {
                <button @onclick="BeginAdd" type="button" class="mr-2 btn btn-light btn-sm"><i class="fas fa-plus"></i> 添加</button>
            }
            @if (IsRefersh)
            {
                <button @onclick="e => Refersh()" type="button" class="mr-2 btn btn-light btn-sm"><i class="fas fa-sync-alt"></i> 刷新</button>
            }

            @CardHeader

            <div class="float-end">
                @if (IsSearchText)
                {
                    <AdminSearchText AdminQuery="q" />
                }
            </div>
        </div>

    }
    <div class="card-body p-0" style="border:none;@(BodyHeight > 0 ? $"height:{BodyHeight}px;margin-top:0.5rem;overflow:auto;" : "")">
        <div class="@(is1r4c ? "" : "table-responsive ")">
            <table class="table @(is1r4c && treeNav.Key.IsNull() ? "" : "table-hover ")table-bordered table-sm m-0">
                <thead style="@(is1r4c ? "display:none " : "")">
                    <tr>
                        @if (IsSingleSelect || IsMultiSelect)
                        {
                            <th style="@(TableTd1 == null && TableTh1 == null ? "width:60px;" : "")" colspan="@(is1r4c ? Colspan : 1)">
                                @if (IsSingleSelect == false)
                                {
                                    <span class="icheck-primary sm"><input @onchange="e => CheckboxOnChange(null, e.Value.ConvertTo<bool>())" type="checkbox"></span>
                                }
                                else
                                {
                                    <span>&nbsp;</span>
                                }
                                @if (TableTh1 != null)
                                {
                                    <span class="mr-2"></span>
                                    @TableTh1
                                }
                            </th>
                        }
                        @TableHeader
                        @if (TableTd99 != null || IsEdit || IsRemove)
                        {
                            <th width="@TableTd99Width">&nbsp;</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @if (is1r4c)
                    {
                        if (treeNav.Key.IsNull() || !items.Any(a => a.Level > 1))
                        {
                            @* 一行四列 *@
                            for (var x = 0; x < items.Count; x += Colspan)
                            {
                                <tr>
                                    @for (var y = 0; y < Colspan; y++)
                                    {
                                        var opt = x + y < items.Count ? items[x + y] : null;
                                        <td width="@(100/Colspan)%" class="pl-3">
                                            @if (opt != null)
                                            {
                                                if (IsSingleSelect || IsMultiSelect)
                                                {
                                                    if (IsSingleSelect == false)
                                                    {
                                                        <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="checkbox" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" /></span>
                                                    }
                                                    else
                                                    {
                                                        <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="radio" name="@(tempid + "_selectRadio")" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" /></span>
                                                    }
                                                }
                                                <label for="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" class="form-check-label pl-2">@TableTd1(opt.Value)</label>
                                            }
                                            else
                                            {
                                                <span>&nbsp;</span>
                                            }
                                        </td>
                                    }
                                </tr>
                            }
                        }
                        else
                        {
                            for (var x = 0; x < items.Count; x++)
                            {
                                var opt = items[x];
                                var ischild = x + 1 < items.Count ? items[x + 1].Level > opt.Level : false;
                                <tr>
                                    <td style="@(opt.Level > 1 ? $"padding-left:{opt.Level * 1.53}rem" : "")" @onclick="e => RowClick(opt)">
                                        @if (opt != null)
                                        {
                                            if (IsSingleSelect || IsMultiSelect)
                                            {
                                                if (IsSingleSelect == false)
                                                {
                                                    <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="checkbox" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" /></span>
                                                }
                                                else
                                                {
                                                    <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="radio" name="@(tempid + "_selectRadio")" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" /></span>
                                                }
                                            }
                                            <label for="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt.Value))" class="form-check-label pl-2">@TableTd1(opt.Value)</label>
                                        }
                                        else
                                        {
                                            <span>&nbsp;</span>
                                        }
                                    </td>
                                </tr>
                                var childInRows = 0;
                                for (var y = x + 1; y < items.Count; y++)
                                {
                                    if (items[y].Level == opt.Level + 1) 
                                    {
                                        childInRows = y;
                                        continue;
                                    }
                                    if (items[y].Level > opt.Level + 1)
                                    {
                                        childInRows = 0;
                                        break;
                                    }
                                    break;
                                }
                                if (x < childInRows)
                                {
                                    <tr>
                                        <td style="padding-left:@(items[x + 1].Level * 1.53)rem">
                                            @for (var y = x + 1; y <= childInRows; y++)
                                            {
                                                var opt2 = items[y];
                                                if (IsSingleSelect || IsMultiSelect)
                                                {
                                                    if (IsSingleSelect == false)
                                                    {
                                                        <span class="icheck-primary sm"><input checked="@opt2.Selected" @oninput="e => CheckboxOnChange(opt2, e.Value.ConvertTo<bool>())" type="checkbox" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt2.Value))" /></span>
                                                    }
                                                    else
                                                    {
                                                        <span class="icheck-primary sm"><input checked="@opt2.Selected" @oninput="e => CheckboxOnChange(opt2, e.Value.ConvertTo<bool>())" type="radio" name="@(tempid + "_selectRadio")" id="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt2.Value))" /></span>
                                                    }
                                                }
                                                <label for="@(tempid + "selectRadio_" + GetItemPrimaryValue(opt2.Value))" class="form-check-label pl-2 mr-4">@TableTd1(opt2.Value)</label>
                                            }
                                        </td>
                                    </tr>
                                    x = childInRows;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (var x = 0; x < items.Count; x++)
                        {
                            var opt = items[x];
                            if (OnSelectChanged.HasDelegate || OnRowClick.HasDelegate)
                            {
                                <tr @onclick="e => RowClick(opt)" class="@opt.RowClass">
                                    @if (IsSingleSelect || IsMultiSelect)
                                    {
                                        <td style="@(opt.Level > 1 ? $"padding-left:{opt.Level * 1.53}rem" : "")">
                                            @if (IsSingleSelect == false)
                                            {
                                                <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="checkbox" /></span>
                                            }
                                            else
                                            {
                                                <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="radio" name="@(tempid + "_selectRadio")" /></span>
                                            }
                                            @if (TableTd1 != null)
                                            {
                                                <span class="mr-2"></span>
                                                @TableTd1(opt.Value)
                                            }
                                        </td>
                                    }
                                    @if (TableRow != null)
                                    {
                                        @TableRow(opt.Value)
                                    }
                                    @if (TableTd99 != null || IsEdit || IsRemove)
                                    {
                                        <td @onclick:stopPropagation="true">
                                            <div class="btn-toolbar">
                                                @if (TableTd99 != null)
                                                {
                                                    @TableTd99(opt.Value)
                                                }
                                                @if (IsEdit)
                                                {
                                                    <button @onclick="e => BeginEdit(opt?.Value)" type="button" class="mr-2 btn btn-light btn-xs"><i class="fa fa-edit"></i>编辑</button>
                                                }

                                                @if (IsRemove && IsRowRemove)
                                                {
                                                    <button @onclick="e => Delete(opt.Value)" type="button" class="btn btn-light btn-xs"><i class="far fa-trash-alt"></i>删除</button>
                                                }
                                            </div>
                                        </td>
                                    }
                                </tr>
                            }
                            else
                            {
                                <tr>
                                    @if (IsSingleSelect || IsMultiSelect)
                                    {
                                        <td style="@(opt.Level > 1 ? $"padding-left:{opt.Level * 1.53}rem" : "")">
                                            @if (IsSingleSelect == false)
                                            {
                                                <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="checkbox" /></span>
                                            }
                                            else
                                            {
                                                <span class="icheck-primary sm"><input checked="@opt.Selected" @oninput="e => CheckboxOnChange(opt, e.Value.ConvertTo<bool>())" type="radio" name="@(tempid + "_selectRadio")" /></span>
                                            }
                                            @if (TableTd1 != null)
                                            {
                                                <span class="mr-2"></span>
                                                @TableTd1(opt.Value)
                                            }
                                        </td>
                                    }
                                    @if (TableRow != null)
                                    {
                                        @TableRow(opt.Value)
                                    }
                                    @if (TableTd99 != null || IsEdit || IsRemove)
                                    {
                                        <td @onclick:stopPropagation="true">
                                            <div class="btn-toolbar">
                                                @if (TableTd99 != null)
                                                {
                                                    @TableTd99(opt.Value)
                                                }
                                                @if (IsEdit)
                                                {
                                                    <button @onclick="e => BeginEdit(opt?.Value)" type="button" class="mr-2 btn btn-light btn-xs"><i class="fa fa-edit"></i>编辑</button>
                                                }

                                                @if (IsRemove && IsRowRemove)
                                                {
                                                    <button @onclick="e => Delete(opt.Value)" type="button" class="btn btn-light btn-xs"><i class="far fa-trash-alt"></i>删除</button>
                                                }
                                            </div>
                                        </td>
                                    }
                                </tr>
                            }
                        }
                    }
                </tbody>
            </table>
        </div>
    </div>
    @if (q.PageSize > 0 || CardFooter != null)
    {
        <div class="card-footer">
            @if (q.PageSize > 0)
            {
                <AdminPagination AdminQuery="q" />
            }

            @CardFooter

            @* @if (OnSelected.HasDelegate)
            {
                <div class="float-right">
                    @if (selectedCount > 0)
                    {
        <button @onclick="e => { _selectedSingleValue = 0; items.ForEach(a => a.Selected = false); }" type="button" class="ml-2 btn btn-light"><i class="far fa-square"></i> 重置</button>
                        <button @onclick="SelectFinish" type="button" class="ml-2 btn btn-success"><i class="fas fa-plus"></i> 确认选择 @selectedCount 项</button>
                    }
                    else
                    {
        <button type="button" class="ml-2 btn btn-light disabled"><i class="far fa-square"></i> 重置</button>
                        <button type="button" class="ml-2 btn btn-success disabled"><i class="fas fa-plus"></i> 确认选择 @selectedCount 项</button>
                    }
                </div>
            } *@
        </div>
    }
</div>

@if (IsAdd || IsEdit)
{
    <AdminModal Visible="item != null" OnClose="e => item = null" OnYes="Save" DialogClassName="@DialogClassName" Title="@(itemIsEdit ? $"【编辑】{Title}" : $"【添加】{Title}")">
        @if (EditTemplate != null)
        {
            if (item != null)
            {
                @EditTemplate(item)
            }
        }
        else
        {
            <span>请设置 &lt;EditTemplate&gt; 编辑内容</span>
        }
    </AdminModal>
}

@if (IsDebug)
{
    <input type="checkbox" @bind="q.IsQueryString" id="check_IsQueryString" /><label for="check_IsQueryString" class="form-check-label">IsQueryString</label>
    <input type="checkbox" @bind="IsRemove" id="check_IsRemove" /><label for="check_IsRemove" class="form-check-label">IsRemove</label>
    <input type="checkbox" @bind="IsAdd" id="check_IsAdd" /><label for="check_IsAdd" class="form-check-label">IsAdd</label>
    <input type="checkbox" @bind="IsEdit" id="check_IsEdit" /><label for="check_IsEdit" class="form-check-label">IsEdit</label>
    <input type="checkbox" @bind="IsSearchText" id="check_IsSearchText" /><label for="check_IsSearchText" class="form-check-label">IsSearchText</label>
    <input type="checkbox" @bind="IsSingleSelect" id="check_IsSingleSelect" /><label for="check_IsSingleSelect" class="form-check-label">IsSingleSelect</label>
    <input type="checkbox" @bind="IsConfirmEdit" id="check_IsConfirmEdit" /><label for="check_IsConfirmEdit" class="form-check-label">IsConfirmEdit</label>
    <input type="checkbox" @bind="IsConfirmRemove" id="check_IsConfirmRemove" /><label for="check_IsConfirmRemove" class="form-check-label">IsConfirmRemove</label>
}

